﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Model;
using iSun.Common;

namespace iSun.BLL.Base
{
    public static class EntityExtend
    {
        public static int getGuid(this BaseEntity entity)
        {
            Type t = entity.GetType();
            try
            {
                foreach (Attribute attr in t.GetCustomAttributes(true))
                {
                    iSun.Model.TableAttribute tableattribute = attr as iSun.Model.TableAttribute;
                    if (null != tableattribute)
                    {
                        return (int)Globals.GetPropertyValue(entity, tableattribute.Guid);
                    }
                }
            }
            catch { }
            return 0;
        }

        public static string getKey(this BaseEntity entity)
        {
            if (entity is DataTableBaseEntity)
            {
                //entity
                BaseBussnies bll = new BaseBussnies(entity);
                return bll.GetTableKey(getTableName(entity));
            }
            Type t = entity.GetType();
            foreach (Attribute attr in t.GetCustomAttributes(true))
            {
                iSun.Model.TableAttribute tableattribute = attr as iSun.Model.TableAttribute;
                if (null != tableattribute)
                {
                    return tableattribute.Guid;
                }
            }
            return "";
        }

        public static string getTableName(this BaseEntity entity)
        {
            if (entity is DataTableBaseEntity)
            {
                return entity.GetTableName();
            }
            Type t = entity.GetType();
            foreach (Attribute attr in t.GetCustomAttributes(true))
            {
                iSun.Model.TableAttribute tableattribute = attr as iSun.Model.TableAttribute;
                if (null != tableattribute)
                {
                    return tableattribute.TableName;
                }
            }
            return "";
        }


        /// <summary>
        /// 数据保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public static bool SaveEntity(this BaseEntity entity)
        {
            CommBLL bllInstance = new CommBLL(entity);
            return bllInstance.Save();
        }

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public static bool DeleteEntity(this BaseEntity entity)
        {
            CommBLL bllInstance = new CommBLL(entity);
            return bllInstance.Delete();
        }

        public static bool DeleteEntitys(this BaseEntity entity, string Condition)
        {
            CommBLL bllInstance = new CommBLL(entity);
            return bllInstance.Delete(Condition);

        }

        public static Guid? GetKeyFieldValue(this BaseEntity entity)
        {
            return iSun.Common.Globals.GetPropertyValue(entity, entity.getKey()) as System.Guid?;
        }
        //快速定位特性
        public static void LoadDetailData(this BaseEntity entity)
        {
            
        }
    }
}
